package com.qiuyuliantu.base.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.qiuyuliantu.common.core.domain.entity.SysUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.qiuyuliantu.common.annotation.Log;
import com.qiuyuliantu.common.core.controller.BaseController;
import com.qiuyuliantu.common.core.domain.AjaxResult;
import com.qiuyuliantu.common.enums.BusinessType;
import com.qiuyuliantu.base.domain.QiuAccountingRecord;
import com.qiuyuliantu.base.service.IQiuAccountingRecordService;
import com.qiuyuliantu.common.utils.poi.ExcelUtil;
import com.qiuyuliantu.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 记账记录Controller
 *
 * @author lmx
 * @date 2025-07-28
 */
@RestController
@RequestMapping("/base/record")
public class QiuAccountingRecordController extends BaseController
{
    @Autowired
    private IQiuAccountingRecordService qiuAccountingRecordService;

    /**
     * 查询记账记录列表
     */
    @PreAuthorize("@ss.hasPermi('base:record:list')")
    @GetMapping("/list")
    public TableDataInfo list(QiuAccountingRecord qiuAccountingRecord)
    {
        startPage();
        List<QiuAccountingRecord> list = qiuAccountingRecordService.selectQiuAccountingRecordList(qiuAccountingRecord);
        return getDataTable(list);
    }

    /**
     * 导出记账记录列表
     */
    @PreAuthorize("@ss.hasPermi('base:record:export')")
    @Log(title = "记账记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, QiuAccountingRecord qiuAccountingRecord)
    {
        List<QiuAccountingRecord> list = qiuAccountingRecordService.selectQiuAccountingRecordList(qiuAccountingRecord);
        ExcelUtil<QiuAccountingRecord> util = new ExcelUtil<QiuAccountingRecord>(QiuAccountingRecord.class);
        util.exportExcel(response, list, "记账记录数据");
    }

    @Log(title = "账单管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('base:record:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<QiuAccountingRecord> util = new ExcelUtil<QiuAccountingRecord>(QiuAccountingRecord.class);
        List<QiuAccountingRecord> recordList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = qiuAccountingRecordService.importRecord(recordList, updateSupport, operName);
        return success(message);
    }

    /**
     * 下载记账导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<QiuAccountingRecord> util = new ExcelUtil<QiuAccountingRecord>(QiuAccountingRecord.class);
        util.importTemplateExcel(response, "账单数据");
    }

    /**
     * 获取记账记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('base:record:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(qiuAccountingRecordService.selectQiuAccountingRecordById(id));
    }

    /**
     * 新增记账记录
     */
    @PreAuthorize("@ss.hasPermi('base:record:add')")
    @Log(title = "记账记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QiuAccountingRecord qiuAccountingRecord)
    {
        return toAjax(qiuAccountingRecordService.insertQiuAccountingRecord(qiuAccountingRecord));
    }

    /**
     * 修改记账记录
     */
    @PreAuthorize("@ss.hasPermi('base:record:edit')")
    @Log(title = "记账记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QiuAccountingRecord qiuAccountingRecord)
    {
        return toAjax(qiuAccountingRecordService.updateQiuAccountingRecord(qiuAccountingRecord));
    }

    /**
     * 删除记账记录
     */
    @PreAuthorize("@ss.hasPermi('base:record:remove')")
    @Log(title = "记账记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(qiuAccountingRecordService.deleteQiuAccountingRecordByIds(ids));
    }
}
